package com.huizhonglm.diap.ui.shop;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bruce.common.utils.EmptyUtil;
import com.huizhonglm.diap.R;
import com.huizhonglm.diap.api.model.shop.ShopInfo;
import com.huizhonglm.diap.core.image.Thumb;

import java.util.List;

/**
 * 附近商品列表适配器
 *
 * @author LiXiaoLong
 * @version 1.0
 * @since 2016-03-07 13:42
 */
public class SearchAdapter extends BaseAdapter {

    private LayoutInflater inflater;
    private List<ShopInfo> data;

    public SearchAdapter(LayoutInflater inflater, List<ShopInfo> data) {
        this.inflater = inflater;
        this.data = data;
    }

    public void setData(List<ShopInfo> data){
        this.data = data;
    }

    @Override
    public int getCount() {
        return EmptyUtil.isEmpty(data) ? 0 : data.size();
    }

    @Override
    public ShopInfo getItem(int position) {
        return EmptyUtil.isEmpty(data) ? null : data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {
        ViewHolder holder;
        if (view == null) {
            holder = new ViewHolder();
            view = inflater.inflate(R.layout.item_search, parent, false);
            holder.logo = (ImageView) view.findViewById(R.id.shop_logo_img);
            holder.name = (TextView) view.findViewById(R.id.shop_name_txt);
            holder.desc = (TextView) view.findViewById(R.id.shop_desc_txt);
            holder.distance = (TextView) view.findViewById(R.id.shop_distance_txt);

            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }
        ShopInfo item = getItem(position);
        Thumb.getInstance().display(holder.logo, item.getPicPath(), R.drawable.def_logo);
        holder.name.setText(item.getSpName());
        holder.desc.setText(item.getLabel());
        holder.distance.setText(item.getAddress() + item.getDistance() + "m");
        return view;
    }

    private class ViewHolder{
        private ImageView logo;
        private TextView name;
        private TextView desc;
        private TextView distance;
    }
}
